package com.ojoin.trade.common.web.jcaptcha;

import java.awt.Color;
import java.awt.Font;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.util.Random;

public class VerifyCodeImgMaker {

	// 图片和字体大小设置
	private int imageWidth = 100;
	private int imageHeight = 36;
	private StringBuilder VCodes = new StringBuilder(4);

	public VerifyCodeImgMaker() {
        
	}

	public String getVCodes() {
		return VCodes.toString();
	}

	public BufferedImage createImg() {
		BufferedImage img = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_3BYTE_BGR);
		return img;
	}

	public Image imgDraw() {
		// 在这里面画出我们的验证码
		Image img = createImg();
		Graphics2D g = (Graphics2D) img.getGraphics();
		g.setColor(Color.white);
		g.fillRect(0, 0, imageWidth, imageHeight);
		for (int i = 0; i < 4; i++) {
			// 随机生成的code
			String code = randomCode();
			VCodes.append(code);
			// 随机的字号
			int fontSize = randomSize();
			// 随机的字的类型
			String type = randomType();
			// 随机的颜色
			Color fontColor = randomColor();
			g.setFont(new Font(type, fontStyle(), fontSize));
			g.setColor(fontColor);
			g.drawString(code, imageWidth / 4 * i, imageHeight - 10);
			g.drawLine(random.nextInt(imageWidth), random.nextInt(imageHeight), random.nextInt(imageWidth), random.nextInt(imageHeight));
		}
		g.dispose();
		return img;
	}

	public Image getImg() {
		Image img = imgDraw();
		return img;
	}

	private String codes = "123456789qwertyuipasdfghjklzxcvbnmQWERTYUIPASDFGHJKLZXCVBNM";
	private Random random = new Random();

	public String randomCode() {
		char[] codesch = codes.toCharArray();
		String code = codesch[random.nextInt(codesch.length)] + "";
		return code;
	}

	private int[] nums = { 22, 23, 24, 25, 26, 27 };

	public int randomSize() {
		int num = nums[random.nextInt(nums.length)];
		return num;
	}

	private String[] types = {"nyala","Bell MT","Credit valley", "隶书", "幼圆", "魏碑", "宋体", "黑体", "楷体" };

	public String randomType() {
		String type = types[random.nextInt(types.length)];
		return type;
	}

	public Color randomColor() {
		int r = random.nextInt(256);
		int g = random.nextInt(256);
		int b = random.nextInt(256);
		Color fontcolor = new Color(r, g, b);
		return fontcolor;
	}

	public int fontStyle() {
		int b = random.nextInt(4);
		return b;
	}

}
